<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>采购入库</title>
        <link href="../resources/css/ligerui-all.css" rel="stylesheet" type="text/css" /> 
	 	<link href="../resources/css/ligerui-icons.css" rel="stylesheet" type="text/css" /> 
		<script src="../resources/js/jquery-1.9.1.min.js" type="text/javascript"></script>
		<script src="../resources/js/ligerui.min.js" type="text/javascript"></script>
		<script type="text/javascript" src="../resources/js/jquery.validate.min.js"></script>
		<script type="text/javascript" src="../resources/js/jquery.metadata.js"></script>
		<script type="text/javascript" src="../resources/js/messages_cn.js"></script>
		<script type="text/javascript" src="../resources/js/form-validate.js"></script>
            <script type="text/javascript">
            var CustomersData = {Rows:[]};
            
			 $(function ()
                {
	                /*库存表格*/
                	 window['g'] = $("#isr").ligerGrid({
                	 	checkbox: true,
	                  columns: [
	                  { display: '物料编码', name: 'inventoryNO', width: 120, isSort: false},
	                  { display: '物料名称', name: 'inventoryName', width: 100, isSort: false, frozen: true},
	 				  { display: '单位', name: 'unit', width: 100, isSort: false},
	 				  { display: '单位成本', name: 'unitPrice', width: 100, isSort: false},
	 				  { display: '当前库存', name: 'inventoryAmount', width: 100, isSort: false }
	                 ], //width: 1000,
	                 url:"/baohua/inventory/list",
	                 width:300,
	                 pageSize : 5,
	                 pageSizeOptions: [3, 5],
	                 });

	                 /*入库明细*/
	                 window['g_right'] = $("#isr_right").ligerGrid({
                	 	checkbox: true,
	                  columns: [
	                  { display: '物料编码', name: 'rawMaterialNo', width: 100, isSort: false, frozen: true},
	                  { display: '物料名称', name: 'rawMaterialName', width: 150, isSort: false, frozen: true},
	 				  { display: '单位', name: 'unit', width: 100, isSort: false},
	 				  { display: '单位成本', name: 'unitPrice', width: 100, isSort: false},
	 				  { display: '数量', name: 'warehousingAmount', width: 100, isSort: false,type:'float'},
	 				  { display: '总额', name: 'priceAmount', width: 200, isSort: false,
	 					 totalSummary:
	                     {
	 						render: function (suminf, column, cell)
	                        {
		                            return '<div>入库总额:' + suminf.sum + '</div>';
	                        },
	                        align: 'right'
	                     }
	                  }
	                 ], //width: 1000,
	                 data:CustomersData,
	                 width:"99.9%",
	                 pageSizeOptions: [3, 5, 7, 10],
	                 rownumbers :true,
	                 enabledEdit: true
	                 });

	                 /*物料明细中的工作栏*/
		             $("#toolBar").ligerToolBar( {
		                    items : [
										{ text: '删除', click: del, icon: 'busy' }
									]
							
		             });

	                /*设置布局样式*/
                    $("#layout1").ligerLayout({ leftWidth: 310,height:500, bottomHeight: 110});

                    /*数据校验*/  
                  $.metadata.setType("attr", "validate");
		          
		          $("#form0").validate({
		         	 errorPlacement: errorPlacement,
		         	 success: mess
		          });

		          $("#form1").validate({
			         	 errorPlacement: errorPlacement,
			         	 success: mess
			          });

		          /*为form1增加样式*/
		          $("#form1").ligerForm();
		          
		          init();
				/*根据物料名称查询库存物料*/
		      	$("#search").click(function() {
			      	var inventoryName = $("#inventoryName").val();
					var url = "/baohua/inventory/list?inventoryName=" + inventoryName;
					$.post(url, 
							{'page':1, 'pagesize': 10}
					  , function(data){
							g.loadData(data);
							g.setOptions({'url': url, 'pageSizeOptions': [3, 5], 'page':1, 'pagesize': 5} );
						 });
					});
                 /*采购库存*/
		      	$("#purchase").click(function() {
		      		var s = g.getSelectedRows();
		        	 if(s.length > 1) {
		        		 $.ligerDialog.warn('采购信息不允许多选!');
		             }else if(s.length == 0) {
		            	 $.ligerDialog.warn('请选择一条记录!');
		             }else {
				          var amount = parseFloat($("#warehousingAmount").val()).toFixed(2);
				          var priceamount = s[0].unitPrice * amount;

				          var arr = new Array();
				          $(CustomersData.Rows).each(function(i){
					             arr.push(CustomersData.Rows[i].rawMaterialName);
					       });
				          if($.inArray(s[0].inventoryName,arr)==-1) {
					          	 var instoreData = {'rawMaterialNo':s[0].inventoryNO, 'rawMaterialName':s[0].inventoryName,
							           'unit':s[0].unit, 'unitPrice':s[0].unitPrice, 'warehousingAmount':amount,
							           'priceAmount':priceamount};
					            	 CustomersData.Rows.push(instoreData);
					            	 g_right.loadData(CustomersData);
				          }else {
				        	  $.ligerDialog.warn('物料已存在明细中, 请删除重新添加!');
					      }
				          $("#warehousingAmount").val("1.00");
		             }
			      });
		          /*保存采购单*/
		          $("#save").click(function() {
					  init();
					  var arr = CustomersData.Rows;
					  var arr_no = new Array();
					  var arr_amount = new Array();
 					  for(var i=0; i<arr.length; i++) {
 	 					  arr_no.push(arr[i].rawMaterialNo);
 	 					  arr_amount.push(arr[i].warehousingAmount);
 	 				  }
						  
					  var inStoreNO = $("#inStoreNO").val();
					  var warehousingDate = $("#warehousingDate").val();
					  var staffNO = $("#staffNO").val();
					  var memo = $("#memo").val();
					  var status = $("#status").val();
					  var str =  {'inStoreNO': inStoreNO, 'warehousingDate': warehousingDate, 'status':status,'staffNO': staffNO, 'memo': memo, 'arr_no': arr_no.join(","), 'arr_amount': arr_amount.join(",")};
					  var url = "addInstore?" + $.param(str);
					 $.post(url, function(data) {
						 var mess = eval(data);
	            		  switch (mess)
	                      {
	                          case "success":
	                        	 window.location.href="/baohua/instore/index0";
	                              break;
	                          case "error":
	                              $.ligerDialog.warn('请输入正确日期！');
	                              break;
	                          case "systemerror" :
	                        	  $.ligerDialog.warn('物料编号已存在！');
	                        	  break;
	                          case "error1" :
	                        	  $.ligerDialog.warn('物料明细不能为空！');
	                        	  break;
	                      }
					 });
		          });
				 /*增加库存中的新物料*/
		          $("#add_inventory").click(add_inventory);
                });

			 /*删除物料明细的信息*/
			 var del = function() {
				 var s = g_right.getSelectedRows();
				 CustomersData.Rows.splice($.inArray(s[0],CustomersData.Rows),1);
				 g_right.loadData();
				 }
			 
             /*初始化数据, 为编号和数量赋值, 因为form0是一个大表单, 如果增加ligerForm,
                                        势必会影响form0中grid的样式, 故采用单独加样式*/
                var init = function(){
                  $("#inStoreNO").val("ISNO" + new Date().getTime());
                  $("#inventoryNOCopy").val("BH" + new Date().getTime());
		          $("#warehousingAmount").val("1.00");
		          $("#warehousingDate").ligerDateEditor({'width':'180'});
		          $("#warehousingAmount").ligerSpinner({'width':'110'});
                };
                /*增加库存中的新物料（方法）*/
                var add_inventory = function() {
                	loadDirctionary();
                	 $("#add_inventoryNO").val($("#inventoryNOCopy").val());
                	 $("#form1").prop("action", "add");
                	$.ligerDialog.open({ target: $("#frm"), width: 650, title: '增加物料信息' });
                 };

                 /*从数据字典中加载数据*/
                 function loadDirctionary() {
                	   var promise = $.when($.get("/baohua/materiel/loadDirctionary/26"
                	      	   ),$.get("/baohua/materiel/loadDirctionary/31"));
                      promise.done(function(data1, data2) {
                    	   var units = data1[0];
                    	   var categorys = data2[0];
                    	   var unit = "<select name='unit' id='unit' ltype='select' style='width:182px'>";
                    	   $(units).each(function(i) {
                    		   unit = unit + "<option value=" + units[i] + ">" + units[i] + "</option>"
                           });
                           unit = unit + "</select>";
                    	   $("#my_materielUnit").html(unit);
                    	   var category = "<select name='materielCostCategory' ltype='select' id='materielCostCategory' style='width:182px'>";
                    	   $(categorys).each(function(i) {
                        	   category = category + "<option value=" + categorys[i] + ">"+ categorys[i] +"</option>";
                        	});
                    	   category = category + "</select>";
                    	   $("#my_materielCostCategory").html(category);
                    	   $("#unit").ligerComboBox({'width':'180'});
                  	       $("#materielCostCategory").ligerComboBox({'width':'180'});
                       });
                   }
                
         </script> 
        <style type="text/css"> 

            body{ padding:5px; margin:0; padding-bottom:15px;}
            .l-table-edit-td{ padding:3px;}
            .l-button-submit,.l-button-reset{width:80px; float:left; margin-left:10px; padding-bottom:2px;}
            .l-verify-tip{ left:230px; top:120px;}
            #layout1{  width:100%;margin:0; padding:0;  }  
            .l-page-top{ height:80px; background:#f8f8f8; margin-bottom:3px;}
            h4{ margin:20px;}
			.add_text {    
				width: 180px;
			    background: none repeat scroll 0 0 white;
			    border: 1px solid #AECAF0;
			    height: 20px;
			    line-height: 20px;
			    position: relative;
			    margin: 0;
			    padding: 0;
			  }
           </style>
    </head>
    <body style="padding:10px">
     <form name="form0" method="post"  id="form0">
      <div id="layout1">
      <!-- 左布局 -->
            <div position="left" title="采购物料">
			        <table cellpadding="0" cellspacing="0" width="300px">
			        	<tr>
			                <td align="right" class="l-table-edit-td" width="120px">物料编码:</td>
			                <td align="left" class="l-table-edit-td" width="120px">
		                       <input name="inventoryNO" type="text" ltype="text" id="inventoryNOCopy" style="width: 110px" class="add_text" validate="{required:true,minlength:3}"/>
		                    </td>
		                    <td align="left" width="30px"></td>
			                <td align="left" class="l-table-edit-td" width="30px">
			                    <input type="button" value="添加新物料"  id="add_inventory" class="l-button l-button-submit" /> 
			                </td>
			            </tr>
			           <tr>
			                <td align="right" class="l-table-edit-td" width="120px">数量:</td>
			                <td align="left" class="l-table-edit-td" width="110px">
		                       <input name="warehousingAmount" type="text" ltype="spinner" id="warehousingAmount" style="width: 110px" validate="{required:true,min:0.01}"/>
		                    </td>
		                     <td align="left" width="30px"></td>
			                <td align="left" class="l-table-edit-td" width="30px">
			                    <input type="button" value="采购" id="purchase" class="l-button l-button-submit" /> 
			                </td>
			            </tr>
			            <tr>
				                <td align="right" class="l-table-edit-td" width="120px">物料名称:</td>
				                <td align="left" class="l-table-edit-td" width="110px">
			                       <input name="inventoryName" type="text" ltype="text" id="inventoryName" style="width: 110px" class="add_text"/>
			                    </td>
			                     <td align="left" width="30px"></td>
				                <td align="left" class="l-table-edit-td" width="30px">
				                    <input type="button" value="查询" id="search" class="l-button l-button-submit" /> 
				                </td>
			            </tr>
			        </table>
			    <!-- 库存的数据(grid) -->
            	<div id="isr"></div>
            </div>
            <!-- 中间布局 -->
            <div position="center" title="物料明细">
               <!-- 工具条 -->
                <div id="toolBar"></div>
                <!-- 添加的物料明细 -->
            	<div id="isr_right"></div>
            </div>
           <!-- 上布局 -->
            <div position="top">
            	<h3 align="center" style="padding-top: 5px">采购入货</h3>
            	<table cellpadding="0" cellspacing="0" width="100%">
		            <tr>
		                <td align="right" class="l-table-edit-td" width="10%">采购单号:</td>
		                <td align="left" class="l-table-edit-td" width="19%">
		                   <input name="inStoreNO" type="text" ltype="text" id="inStoreNO" class="add_text" readonly="readonly"/>
		                </td>
		                <td align="left" width="21%"></td>
		                <td align="right" class="l-table-edit-td" width="10%" >采购日期:</td>
		                <td align="left" class="l-table-edit-td" style="width:23px" width="19%">
		                   <input name="warehousingDate" type="text" ltype="date" id="warehousingDate" validate="{required:true}"/>
		                   <input name="status" type="hidden" id="status" value="0"/>
		                </td>
		                <td align="left" width="21%"></td>
		             </tr>
		        </table>
            </div>
           <!-- 下布局 -->
            <div position="bottom">
            	<table cellpadding="0" cellspacing="0" width="100%">
		            <tr>
		                <td align="right" class="l-table-edit-td" width="11%">经办人:</td>
		                <td align="left" class="l-table-edit-td" style="width:23px">
		                   <input name="staffNO" id="staffNO" type="text" value="admin" ltype="text" readonly="readonly" class="add_text"/>
		                </td>
		                <td align="right" class="l-table-edit-td" width="11%">&nbsp;</td>
		                <td align="left" class="l-table-edit-td" style="width:23px">&nbsp;</td>
		             </tr>
		             <tr>
		                <td align="right" class="l-table-edit-td" width="11%" class="add_text">备注:</td>
		                <td align="left" class="l-table-edit-td" style="width:23px" colspan="3">
		                   <textarea cols="100" rows="2" class="l-textarea" name="memo" id="memo"></textarea>
		                </td>
		             </tr>
		             <tr>
		                <td align="center" class="l-table-edit-td" style="width:23px" colspan="3">
		                </td>
		                 <td align="right">
	                  <input type="button" value="保存" id="save" class="l-button l-button-submit" /> 
					  <input type="reset" value="取消" class="l-button l-button-reset"/>
	               </td>
		             </tr>
		        </table>
            </div>
        </div> 
      </form>
    <!-- 增加物料信息(弹框) -->
      <div id="frm" style="width:600px; margin:3px; display:none;">
     	<form name="form1" method="post"  id="form1">
        <table cellpadding="0" cellspacing="0" width="600px">
            <tr>
                <td align="right" class="l-table-edit-td" width="100px">物料编码:</td>
                <td align="left" class="l-table-edit-td" style="width:160px"><input name="inventoryNO" type="text" ltype="text" id="add_inventoryNO" validate="{required:true,minlength:3}"/></td>
                <td align="left" width="40px"></td>
                
                <td align="right" class="l-table-edit-td" width="100">物料名称:</td>
                <td align="left" class="l-table-edit-td" style="width:160px"><input name="inventoryName" type="text" ltype="text"  id="add_inventoryName" validate="{required:true}"/></td>
                <td align="left" width="40px"></td>
            </tr>
             
            <tr>
                <td align="right" class="l-table-edit-td" width="100px">物料单位:</td>
                <td align="left" class="l-table-edit-td" style="width:160px" id="my_materielUnit">
                </td>
                <td align="left" width="40px"></td>
                
                <td align="right" class="l-table-edit-td" width="100px">单位成本:</td>
                <td align="left" class="l-table-edit-td" style="width:160px"><input name="unitPrice" type="text" id="add_unitPrice" ltype="spinner"></td>
                <td align="left" width="40px"></td>
            </tr>
            
              <tr>
                 <td align="right" class="l-table-edit-td" width="100px">物料类型:</td>
                <td align="left" class="l-table-edit-td" id="my_materielCostCategory"> 
                </td>
                <td align="left" width="40px"></td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr> 
                 
            <tr>
               <td colspan="4">&nbsp;</td>
               <td align="right">
                  <input type="submit" value="提交"  class="l-button l-button-submit" /> 
				  <input type="reset" value="重置" class="l-button l-button-reset"/>
               </td>
                <td align="left" width="40px"></td>
            </tr>
        </table>
    </form>
    </div>
    </body>
    </html>